<%--
  Created by IntelliJ IDEA.
  User: 86181
  Date: 2022/10/17
  Time: 22:52
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
    <link rel="stylesheet" href="css/bootstrap.css">
    <link rel="stylesheet" href="css/bootstrap-extend.css">
    <link rel="stylesheet" href="css/bootstrap-theme.css">
    <script src="js/jquery.min.js"></script>
    <script src="js/bootstrap.js"></script>
    <script src="js/jquery.form.js"></script>
    <script src="js/jquery.validate.js"></script>
    <script src="js/messages_zh.js"></script>
    <script src="js/bootstrap-table.js"></script>
    <script src="js/bootstrap-table-zh-CN.js"></script>
    <script src="js/echarts.js"></script>
    <script src="js/bootstrap-extend.js"></script>
    <script src="js/bootstrap.min.js"></script>

</head>
</head>
<body>
<div class="container" style="      height:40%">
    <div id="toolbar">
        <form class="form-inline">
            <div class="form-group">
                <label class="control-label" for="rolename">角色名称:</label>
                <input type="text" class="form-control" id="rolename" name="rolename">
            </div>
            <input type="button" onclick="queryrole()" class="btn btn-success" value="查询">
            <input type='button' value='给角色添权限' class='btn btn-danger' onclick='addroleauthorization()'>
            <input type='button' value='给角色添菜单' class='btn btn-danger' onclick='addrolemanu()'>
        </form>
        <input type="button" class="btn btn-info" onclick="addrole()" value="添加角色">
    </div>
    <table id="roletab"></table>
</div>

<!--添加模态框-->
<!--第一个div-->
<div id="addrolemodal" class="modal">
    <!--第二个div-->
    <div class="modal-dialog">
        <!--第三个div-->
        <div class="modal-content">

        </div>
    </div>
</div>

<!--修改模态框-->
<div id="updaterolemodal" class="modal">
    <div class="modal-dialog">
        <div class="modal-content">

        </div>
    </div>
</div>

<div id="addroleauthorization" class="modal">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button class="close" data-dismiss="modal">&times;</button>
                <h4 class="modal-title">用户赋予角色</h4>
            </div>
            <div class="modal-body">
                <div>
                    <label>用户名:</label><span id="name"></span><input type="hidden" id="roleid">
                    <div id="roleauthorization"></div>
                    <div class="modal-footer">
                        <div class="row">
                            <div class="col-md-2 col-md-offset-5">
                                <input type="button" class="btn btn-success" value="角色赋予权限" onclick="roleauthorizationadd()">
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<div id="addrolemenus" class="modal">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button class="close" data-dismiss="modal">&times;</button>
                <h4 class="modal-title">赋予角色菜单</h4>
            </div>
            <div class="modal-body">
                <label class="control-label">角色名：</label>
                <input type="hidden" id="rolesid">
                <span id="rolesname"></span>
                <div id="rolemenudiv">
                </div>
            </div>
            <div class="modal-footer">
                <input type="button" class="btn btn-success" value="确定授予" onclick="roleAddmenu()">
            </div>
        </div>
    </div>
</div>
<script>
    $("#roletab").bootstrapTable({
        url:"queryroles.action",
        method:"post",
        contentType: "application/x-www-form-urlencoded", //如果用tomcat就要写这个
        toolbar:"#toolbar",//工具按件用那个容器
        striped:true,//是否显示隔行变色
        cache:false,//是否缓存
        pagination:true,//是否显示分页
        sortable:true,//是否显示排序
        sortOrder:"aso",//排序方式
        sidePagination: "server", //分页方式：client客户端分页，server服务端分页（*）
        pageNumber:1,//初始化加载第一页，默认第一页,并记录
        pageSize:5,//每页记录数
        pageList:[5,10,15,20],//可供选择的每页的行数（*）
        search:false,//是否显示输入框
        strictSearch: true,
        showColumns: true,//是否显示所有的列（选择显示的列）
        showRefresh: true,//是否显示刷新按钮
        minimumCountColumns: 2,//最少允许的列数
        clickToSelect:true,//是否启用点击选中行
        uniqueId:"id",//每一行的唯一标识，一般为主键列
        showToggle: true,//是否显示详细视图和列表视图的切换按钮
        cardView:false,//是否显示详细视图
        detailView:false,//是否显示父子表
        queryParams:function(params){
            var role={
                pagesize:params.limit,
                pagenum:(params.offset/params.limit)+1,
                sort:params.sort,
                sortOrder:params.sort,
                rolename:$("#rolename").val(),
            }
            return role;
        },
        columns:[
            {
                field: "",
                title: "",
                checkbox:true
            },{
                field:"id",
                title:"角色id",
            },{
                field:"name",
                title:"角色名称",
            },{
                field:"code",
                title:"角色编号",
            },{
                field: "descn",
                title:"角色说明",
            },{
                field:"",
                title:"操作",
                formatter:function (value,row,index) {
                    return "<input type='button' value='编辑' class='btn btn-warning' onclick='updaterole("+row.id+")'>&nbsp;"+
                        "<input type='button' value='删除' class='btn btn-danger' onclick='delrole("+row.id+")'>"
                }
            }
        ]
    })
    //查询刷新
    function queryrole() {
        $("#roletab").bootstrapTable("refresh")
    }
    //角色添加菜单
    function addrolemenu(id) {
        $("#addrolemenus").modal({
            remote:"addrolemenus.jsp?id="+id
        })
    }
    //添加
    function addrole() {
        $("#addrolemodal").modal({
            remote:"addrole.jsp"
        })
    }
    //修改
    function updaterole(id) {
        //获取模态框
        $("#updaterolemodal").modal({
            remote:"updaterole.jsp?id="+id
        })
    }
    //删除
    function delrole(id) {
        if(confirm("您确定要删除?")){
            $.ajax({
                url:"deleteroles.action/"+id,
                type:"delete",
                dataType:"json",
                success:function (data) {
                    alert(data.msg)
                    location.href="roles.jsp";
                }
            })
        }
    }
    //角色赋予权限
    function addroleauthorization() {
        var arr= $("#roletab").bootstrapTable("getSelections");
        $("#roleauthorization :checkbox").remove()
        $("#roleauthorization span").remove()
        if(arr.length==1){
            var roleid=arr[0].id
            $("#addroleauthorization").modal("show")
            $.get("querybyauall.action/"+roleid,function (data) {
                $("#name").text(data.name)
                $("#roleid").val(data.id)
                $(data.authorizationList).each(function (index,item) {
                    var ck=$("<input type='checkbox' value='"+item.auid+"'><span>"+item.auname+"</span>")
                    $("#roleauthorization").append(ck)
                    if(item.check){
                        ck.prop("checked","checked")
                    }
                })
            },"JSON")
        }else {
            alert("只能选择一条记录，才可以进行赋予");
        }
    }

    function roleauthorizationadd() {
      //  var mz=$("#name").text();
        var roleid=$("#roleid").val()
        var che=$("#roleauthorization :checkbox:checked")
        var ids=[];
        che.each(function (index,item) {
                ids.push({auid:item.value});
        })
        var roles={id:roleid,authorizationList:ids}
        console.log(roles)
        $.ajax({
            url:"addquanxian.action",
            data:JSON.stringify(roles),
            contentType:"application/json",
            type:"post",
            dataType:"json",
            success:function (data) {
                alert(data.msg);
                $("#addroleauthorization").modal("hide")
                $("#roletab").bootstrapTable("refresh")
            }
        })
    }
    //打开赋予菜单模态框
    function addrolemanu() {
        var arr=$("#roletab").bootstrapTable("getSelections");
        if(arr.length==1){
            querybyroleid(arr[0].id);
            $("#addrolemenus").modal("show")
            let param = {
                data : {
                    url : "queryquanxianbyroleid.action/"+arr[0].id,
                    type : 'post',
                },
                process : {
                    open : true,
                    icon : '',
                    //checkbox,如果为undefined将不显示checkbox,
                    //你可以直接写 checkbox : function(item,index,layer){return checkboxObj} ,函数返回一个checkbox对象
                    //当然也可以像下面这样
                    checkbox : {
                        //是否启用checkbox,默认true,可以为boolean值或function(item,index,layer){return boolean},
                        enable : true,
                        //checkbox名称,字段名或者function(item,index,layer){return str},
                        name : 'name',
                        //checkbox值属性,字段名或者function(item,index,layer){return str},
                        value : 'id',
                        //checkbox是否选中,默认false,如果选中,那么该节点的所有子节点都将选中,字段名或者function(item,index,layer){return boolean},
                        checked : function (item) {
                            return item.check;
                        },
                    },
                    //标题(鼠标悬停时显示),字段或者function(item,index,layer){return str},
                    title : undefined,
                    //树节点显示内容,字段或者function(item,index,layer){return str},
                    text : 'name',
                    //子级(list集合),字段名或者function(item,index,layer){return list}
                    child : 'menus',
                    //节点点击事件
                    //可以直接 onChick : function(e,node,type){return void} ,node为当前节点(span)的jquery对象
                    //当然也可以像下面这样
                },
                //节点树是否占满父级节点
                fill : true,
                //字体大小
                fontSize : 15
            }
            //加载菜单数据 放在 modal-body中
            $("#rolemenudiv").loadTree(param);
        }else{
            alert("请选择一个角色，赋予菜单!");
        }
    }
    //角色赋予菜单
    function roleAddmenu() {
        var ck=$("#rolemenudiv :checkbox:checked")
        var menuid=[]
        ck.each(function (index,item) {
            menuid.push({id:item.value})
        })
        var obj={id:$("#rolesid").val(),menus:menuid}
        $.ajax({
            url:"addmenuroles.action",
            data:JSON.stringify(obj),
            contentType:"application/json",
            type:"post",
            dataType:"json",
            success:function (data) {
                alert(data.msg)
                $("#addrolemenus").modal("hide")
                $("#roletab").bootstrapTable("refresh")
            }
        })
    }
    function  querybyroleid(id) {
        $.get("querybyroleid11.action/"+id,function (data) {
            $("#rolesid").val(data.id);
            $("#rolesname").text(data.name)
        },"json")
    }
</script>
</body>
</html>
